package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private BlackListApi blackListApi;  //黑名单

    @DubboReference
    private QuestionApi questionApi;    //问题表

    @DubboReference
    private SettingApi settingsApi;  //通用设置

    //查询通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //2、获取用户的手机号码
        String mobile = UserHolder.getMobile();
        vo.setPhone(mobile);
        //3、获取用户的陌生人问题
        Question qualifier = questionApi.findByUserId(userId);
        String txt = qualifier == null? "你喜欢java吗?":qualifier.getTxt();
        vo.setStrangerQuestion(txt);
        //4、获取用户的APP通知开关数据
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null){
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    //设置陌生人问题
    public void saveQuestion(String content) {
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();
        //2、调用api查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        //3、判断问题是否存在,不存在保存,存在更新
        if (question == null){
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else {
            question.setTxt(content);
            questionApi.update(question);
        }

    }

    //通知设置
    public void saveSettings(Map map) {
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();
        //2、根据用户id，查询用户的通知设置
        Settings settings = settingsApi.findByUserId(userId);
        //3、判断,不存在保存,存在更新
        if (settings == null){
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification((Boolean) map.get("likeNotification"));
            settings.setPinglunNotification((Boolean) map.get("pinglunNotification"));
            settings.setGonggaoNotification((Boolean) map.get("gonggaoNotification"));
            settingsApi.save(settings);
        }else {
            settings.setLikeNotification((Boolean) map.get("likeNotification"));
            settings.setPinglunNotification((Boolean) map.get("pinglunNotification"));
            settings.setGonggaoNotification((Boolean) map.get("gonggaoNotification"));
            settingsApi.update(settings);
        }
    }

    //黑名单分页查询
    public PageResult blacklist(int page, int pagesize) {
        //获取当前用户的id
        Long userId = UserHolder.getUserId();
        //调用API查询用户的黑名单分页列表  Ipage对象
        IPage<UserInfo> ip= blackListApi.findByUserId(userId,page,pagesize);
        //对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pr = new PageResult(page,pagesize, (int) ip.getTotal(),ip.getRecords());
        //返回
        return pr;
    }

    //取消黑名单
    public void deleteBlackList(Long blackUserId) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用api删除
        blackListApi.delete(userId,blackUserId);
    }
}
